<?php
session_start();
include_once( 'config.php' );
include_once( 'weibooauth.php' );
if(! isset($_SESSION['last_key'])) 
	header("Location: index.php");
$text = $_REQUEST ['caption']?urldecode($_REQUEST ['caption']):'分享图片';
$img_data = $_REQUEST ['img_data'];
$c = new WeiboClient( WB_AKEY , WB_SKEY , $_SESSION['last_key']['oauth_token'] , $_SESSION['last_key']['oauth_token_secret']  );
$Result = $c->upload($text,$img_data);
$Data['status_id'] = $Result['id'];
$Data['user_id'] = $Result['user']['id'];
$Data['text'] = $text;
$mysql = new SaeMysql();
$sql = "INSERT INTO `photos` ( `status_id` , `user_id` , `text` ) VALUES ( '" . 
$mysql->escape( $Data['status_id'] ) . "' , '" . $mysql->escape(  $Data['user_id'] ) .
 "' , '" . $mysql->escape(  $Data['text'] ) . "')";
$mysql->runSql( $sql );	
if( $mysql->errno() != 0 ){
	$Error['error_message'] = 'Mysql error ' . $mysql->errmsg();
	json_encode($Error);
	die();
}
echo json_encode($Result);
die();
